/*
 * IntegerCache.java
 */

package at.momberban.game.me;

/**
 * integer cache for java 1.3, similar to the one available from java 1.5
 * 
 * http://blogs.sun.com/darcy/entry/boxing_and_caches_integer_valueof 
 * 
 */
public class IntegerCache {
    private static final Integer cache[] = new Integer[-(-128) + 127 + 1];

    static {
        for (int i = 0; i < cache.length; i++) {
            cache[i] = new Integer(i - 128);
        }
    }

    /**
     * returns a cached integer 
     * 
     * @param i
     * @return
     */
    public static Integer valueOf(int i) {
        final int offset = 128;
        if (i >= -128 && i <= 127) { // must cache
            return IntegerCache.cache[i + offset];
        }
        return new Integer(i);
    }
    
    /**
     * return a integer from a string
     * 
     * @param str
     * @return
     */
    public static Integer valueOf(final String str) {
        final int value = Integer.parseInt(str);
        return valueOf(value);
    }

}
